import numpy as np

def numerial_grad(f,x):
    h = 1e-4
    grad = np.zeros_like(x)
    for idx in range(x.size):
        temp = x[idx]
        x[idx] = float(temp) + h
        fx1 = f(x)

        x[idx] = float(temp) - h
        fx2 = f(x)
        grad[idx] = (fx1-fx2)/(2*h)
        x[idx] = temp # necessary!!!
    return grad

def numerial_grad_with_batch(f,X):
    if X.ndim == 1:
        return numerial_grad(f,X)
    else:
        h = 1e-4
        grad = np.zeros_like(X)
        X = enumerate(X)
        for idx,x in X:
            grad[idx] = numerial_grad(f,x)
    return grad
